import uvicorn
from fastapi import FastAPI,Query
from typing import Optional
from pydantic import BaseModel

app = FastAPI()


# 请求体模型

# @app.get("/items/")              #通过Query可以对参数进行校验，m的长度超过2则报错
# def update_times(m: Optional[str] = Query(None, max_length=2)):
#     results = {"items": [{"oneid": "shanghai"}, {"two": "beijing"}]}
#     if m:
#         results.update({'shanghai': m})
#     return results


# 如果我们要求最长的不能超过10个字符，最短不能低于5个字符串怎么实现呢。

# @app.get("/items/")
# def update_times(m: Optional[str] = Query(None, max_length=10, min_length=5)):
#     results = {"items": [{"oneid": "shanghai"}, {"two": "beijing"}]}
#     if m:
#         results.update({'shanghai': m})
#     return results

'''
需要匹配参数，参数开头包含name的。如何实现。 通过关键字regex 正则
'''


@app.get("/items/")
async def update_items(m: Optional[str] =  Query(None, regex='^name',max_length=10,min_length=5)):
    results = {"items": [{"oneid": "shanghai"}, {"two": "beijing"}]}
    if m:
        results.update({"shanghai": m})
    return results

if __name__ == '__main__':
    uvicorn.run(app="main2:app", host="127.0.0.1", port=8000, reload=True, debug=True)
